package next.Admin;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabHost.TabSpec;
import next.CustomLayout.TitleBar;
import next.Functionality.S_3R4_Activity;
import next.TemplateActivity.R;

public class Business_Rules extends S_3R4_Activity {

	private TabHost mTabHost;
	private EditText edlevels, edSons, edlevel, edpruduct, edcampaign, edadd, edNO, edNIO, edNP, edNIP;
	private CheckBox Structure, Rules, level, pruduct, campaign, add, extended, NO,NIO,NP,NIP;

	private void setupTabHost() {
		mTabHost = (TabHost) findViewById(android.R.id.tabhost);
		mTabHost.setup();
	}

	/** Creating content custom Tab layout */
	private void setupTab(final View view, final String tag) {
		View tabview = createTabView(mTabHost.getContext(), tag);

		TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview)
				.setContent(new TabContentFactory() {
					public View createTabContent(String tag) {
						View view;
						if (tag.contains("EXTENDED")) {
							view = LayoutInflater
									.from(mTabHost.getContext())
									.inflate(
											R.layout.appa_business_rules_2_layout,
											null);
							// --Assembling Widgets
							edNO = (EditText) view
									.findViewById(R.id.business_rules2_ET1);
							edNIO = (EditText) view
									.findViewById(R.id.business_rules2_ET2);
							edNP = (EditText) view
									.findViewById(R.id.business_rules2_ET3);
							edNIP = (EditText) view
									.findViewById(R.id.business_rules2_ET4);
							
							edNO.setEnabled(false);
							edNIO.setEnabled(false);
							edNP.setEnabled(false);
							edNIP.setEnabled(false);
							
							NO= (CheckBox) view
									.findViewById(R.id.business_rules2_CB1);
							NIO= (CheckBox) view
									.findViewById(R.id.business_rules2_CB2);
							NP= (CheckBox) view
									.findViewById(R.id.business_rules2_CB3);
							NIP= (CheckBox) view
									.findViewById(R.id.business_rules2_CB4);
							NO.setEnabled(false); 
							NIO.setEnabled(false);
							NP.setEnabled(false);
							NIP.setEnabled(false);
							
							
							extended = (CheckBox) view
									.findViewById(R.id.business_rules2_CB0);
							extended.setOnClickListener(S3R4Listener2);
							
							
						} else if (tag.contains("CUSTOM")) {
							view = LayoutInflater
									.from(mTabHost.getContext())
									.inflate(
											R.layout.appa_business_rules_3_layout,
											null);
						} else {
							view = LayoutInflater
									.from(mTabHost.getContext())
									.inflate(
											R.layout.appa_business_rules_1_layout,
											null);
							// --Assembling Widgets
							edlevels = (EditText) view
									.findViewById(R.id.business_rules1_ET1);
							edSons = (EditText) view
									.findViewById(R.id.business_rules1_ET2);
							edlevel = (EditText) view
									.findViewById(R.id.business_rules1_ET3);
							edpruduct = (EditText) view
									.findViewById(R.id.business_rules1_ET4);
							edcampaign = (EditText) view
									.findViewById(R.id.business_rules1_ET5);
							edadd = (EditText) view
									.findViewById(R.id.business_rules1_ET6);
							edlevels.setEnabled(false);
							edSons.setEnabled(false);
							edlevel.setEnabled(false);
							edpruduct.setEnabled(false);
							edcampaign.setEnabled(false);
							edadd.setEnabled(false);
							
							level= (CheckBox) view
									.findViewById(R.id.business_rules1_CB3);
							pruduct= (CheckBox) view
									.findViewById(R.id.business_rules1_CB4);
							campaign= (CheckBox) view
									.findViewById(R.id.business_rules1_CB5);
							add= (CheckBox) view
									.findViewById(R.id.business_rules1_CB6);
							level.setEnabled(false); 
							pruduct.setEnabled(false);
							campaign.setEnabled(false);
							add.setEnabled(false);
							

							Structure = (CheckBox) view
									.findViewById(R.id.business_rules1_CB1);
							Rules = (CheckBox) view
									.findViewById(R.id.business_rules1_CB2);
							Structure.setOnClickListener(S3R4Listener);
							Rules.setOnClickListener(S3R4Listener);

						}
						return view;
					}
				});
		mTabHost.addTab(setContent);

	}

	/** Creating custom Tab layout */
	private static View createTabView(final Context context, final String text) {
		View view;
		view = LayoutInflater.from(context).inflate(
				R.layout.generic_tabs_custom_layout, null);
		TextView tv = (TextView) view.findViewById(R.id.tabsText);
		tv.setText(text);
		return view;
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// construct the tabhost
		setContentView(R.layout.generic_3_tabs_layout);

		// -- Initialize title Bar--
		titleBar = (TitleBar) findViewById(R.id.normal_activity_CV_TB1);
		// -- Call the methods to change the underlying widgets--
		titleBar.setTitle("BUSINESS RULES");
		titleBar.setIcon(R.drawable.icon);
		titleBar.showProgressBar();

		// --Prepare tabs
		setupTabHost();
		mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);

		setupTab(new TextView(this), "COMMON Rules");
		setupTab(new TextView(this), "EXTENDED Rules 2");
		setupTab(new TextView(this), "CUSTOM");
	}

	private OnClickListener S3R4Listener = new OnClickListener() {
		public void onClick(View v) {
			if (Structure.isChecked()) {
				edlevels.setEnabled(true);
				edSons.setEnabled(true);
			} else {
				edlevels.setEnabled(false);
				edSons.setEnabled(false);
			}

			if (Rules.isChecked()) {
				edlevel.setEnabled(true);
				edpruduct.setEnabled(true);
				edcampaign.setEnabled(true);
				edadd.setEnabled(true);
				level.setEnabled(true); 
				pruduct.setEnabled(true);
				campaign.setEnabled(true);
				add.setEnabled(true);
			} else {
				level.setEnabled(false); 
				pruduct.setEnabled(false);
				campaign.setEnabled(false);
				add.setEnabled(false);
				edlevel.setEnabled(false);
				edpruduct.setEnabled(false);
				edcampaign.setEnabled(false);
				edadd.setEnabled(false);
			}
		}
	};
	
	private OnClickListener S3R4Listener2 = new OnClickListener() {
		public void onClick(View v) {
			
			if (extended.isChecked()) {
				edNO.setEnabled(true);
				edNIO.setEnabled(true);
				edNP.setEnabled(true);
				edNIP.setEnabled(true);
				NO.setEnabled(true); 
				NIO.setEnabled(true);
				NP.setEnabled(true);
				NIP.setEnabled(true);
			} else {
				NO.setEnabled(false); 
				NIO.setEnabled(false);
				NP.setEnabled(false);
				NIP.setEnabled(false);
				edNO.setEnabled(false);
				edNIO.setEnabled(false);
				edNP.setEnabled(false);
				edNIP.setEnabled(false);
			}
		}
	};
}